package com.ctg.itrdc.sysmgr.portal.control;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.ctg.itrdc.pasp.core.exception.CodedException;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.pasp.core.PageInfo;
import com.ctg.itrdc.pasp.security.SecUser;
import com.ctg.itrdc.pasp.security.utils.WebUtils;
import com.ctg.itrdc.sysmgr.portal.api.dto.SystemUserDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.ISystemUserService;
import com.ctg.itrdc.sysmgr.portal.common.entity.CommonResult;
import com.ctg.itrdc.sysmgr.portal.utils.DTOHelper;

@Controller
@RequestMapping("/system_user")
public class SystemUserController extends SysmgrCURDController<Long, SystemUserDTO> {

    @Resource
    private ISystemUserService systemUserService;


    @RequestMapping("/page")
    @ResponseBody
    public CommonResult queryPageInfo(SystemUserDTO dto,
                                      @RequestParam(defaultValue = "0") Integer currentPage,
                                      @RequestParam(defaultValue = "10") Integer perPage)
            throws Exception {
        PageInfo<SystemUserDTO> pageInfo = systemUserService.selectByOrgId(dto, currentPage, perPage);
        return new CommonResult(HttpStatus.OK.toString(), "分页查询成功", pageInfo);
    }


    @RequestMapping("/update")
    @ResponseBody
    public CommonResult updateByPrimaryKey(@Valid SystemUserDTO dto, BindingResult result) throws Exception {
        DTOHelper.valid(result);
        // checkLoginUser(dto.getSysUserId());
        DTOHelper.updateDTO(dto);
        // 不更新密碼的情況,使用updateNotNullFieldsByPrimaryKey 排除掉密碼
        if ("******".equals(dto.getPassword())) {
            dto.setPassword(null);
        }
//		int i = systemUserService.updateNotNullFieldsByPrimaryKey(dto);
        int i = systemUserService.updateWithPassword(dto, dto.getPassword());
        return new CommonResult(HttpStatus.OK.toString(), "更新数据成功", i);
    }

    /**
     * @api {post} /system_user/update/password 个人更新密码
     * @apiGroup system_user
     * @apiName updatePassword
     * @apiParam {Long} sysUserId 选传 不传为当前用户
     * @apiParam {String} oldPassword 必传
     * @apiParam {String} newPassword 必传
     * @apiVersion 0.0.1
     */
    @RequestMapping("/update/password")
    @ResponseBody
    public CommonResult updatePassword(
            @RequestParam(required = false) Long sysUserId,
            @RequestParam String oldPassword,
            @RequestParam String newPassword
    ) throws Exception {
//		checkLoginUser(sysUserId);
        String sysUserCode = WebUtils.getCtgUser().getSysUserCode();
        int i = systemUserService.updatePassword(sysUserCode, newPassword, oldPassword);
        // 退出
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            subject.logout();
        }
        return new CommonResult(HttpStatus.OK.toString(), "更新数据成功", i);

    }

    @RequestMapping(value = "/api/password", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updatePasswordApi(
            @RequestParam String username,
            @RequestParam String oldPassword,
            @RequestParam String newPassword) throws Exception {
        int i = 0;
        try {
            i = systemUserService.updatePassword(username, newPassword, oldPassword);
        } catch (CodedException e) {
            switch (e.getCode()){
                case "402":
                    return new CommonResult("402","密码错误!");
                case "404":
                    return new CommonResult("404","用户不存在!");
                case "405":
                    return new CommonResult("405","密码格式错误!");
                case "407":
                    return new CommonResult("407","密码重用错误!");
                case "408":
                    return new CommonResult("408","密码重用错误!");
            }
        }
        // 退出
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            subject.logout();
        }
        return new CommonResult(HttpStatus.OK.toString(), "更新数据成功", i);

    }

    /**
     * @api {post} /system_user/reset/password 重置密码
     * @apiGroup system_user
     * @apiName resetPassword
     * @apiParam {Long} sysUserId 选传 不传为当前用户
     * @apiVersion 0.0.1
     */
    @RequestMapping("/reset/password")
    @ResponseBody
    public CommonResult resetPassword(@RequestParam(required = false) Long sysUserId) throws Exception {
        SystemUserDTO dto = new SystemUserDTO();
        if (sysUserId == null) {
            SecUser ctgUser = WebUtils.getCtgUser();
            sysUserId = ctgUser.getSysUserId();
        }
        dto.setSysUserId(sysUserId);
        return new CommonResult(HttpStatus.OK.toString(), "密码重置成功", systemUserService.resetPassword(dto));
    }

}
